package com.cxb.demotest.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * 文件工具类
 */
@Component("materialFileHelper")
public class FileHelper {


  private static final String MAC_INSTANCE_NAME = "HMacSHA256";

  //上传文档类型定义
  private static final String TYPE_IMAGE = "image";//['jpg', 'png', 'gif', 'bmp', 'jpeg', 'png',]
  private static final String TYPE_DOCUMENT = "document";//['doc', 'docx', 'xls', 'xlsx', 'csv', 'ppt', 'pptx', 'pdf',]
  private static final String TYPE_PACKAGE = "package";//['zip', 'rar', 'tar.gz',]
  private static final String TYPE_VIDEO = "video";//['mp4',]
  private static final String TYPE_MULTI_MEDIA = "multimedia";// ['mp4', 'mp3', 'jpg', 'png', 'gif', 'bmp', 'jpeg', 'png',]

  //上传地址（开发测试和线上环境）
  @Value("${file-service.url}")
  private String fileServiceUrl;

  //客户端
  private static String client = "scm3";
  //默认最大文件大小
  private static int defaultMaxSize = 300;//单位M
  //API请求路径
  private static final String API_PATH = "/api/upload";
  //生效时间
  public static final int DEFAULT_TTL = 24 * 60 * 60 * 1000;//24小时

  public InputStream downloadFile(String httpUrl) throws IOException {
    URL url = null;
    try {
      url = new URL(httpUrl);
    } catch (MalformedURLException e) {
      return null;
    }
    try {
      URLConnection conn = url.openConnection();
      return conn.getInputStream();
    } catch (Exception e) {
      return null;
    }
  }

}
